/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.transas.zornav.file;

import com.transas.zornav.data.DataSource;
import java.io.BufferedWriter;
import java.text.NumberFormat;
import java.util.Locale;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.DOMException;
import org.w3c.dom.DOMImplementation;
import org.w3c.dom.Document;
import org.w3c.dom.Element;


/**
 *
 * @author amalikov
 */
public class TaskDataSourceWriter implements DataSourceWriter {
	private final static Logger LOG = Logger.getLogger(TaskDataSourceWriter.class.getName());

	@Override
	public void process(BufferedWriter file, DataSource ds) {
		try {
			StreamResult streamResult = new StreamResult(file);
			TransformerFactory tf = TransformerFactory.newInstance();
			tf.setAttribute("indent-number", 4); 
			Transformer serializer = tf.newTransformer();
			serializer.setOutputProperty(OutputKeys.METHOD, "xml");
			serializer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
			serializer.setOutputProperty(OutputKeys.INDENT, "yes");
			serializer.transform(createDOMSource(ds), streamResult);
        } catch (TransformerException ex) {
			LOG.log(Level.SEVERE, null, ex);
        } catch (ParserConfigurationException ex) {
            LOG.log(Level.SEVERE, null, ex);
        }
	}

	private DOMSource createDOMSource(DataSource ds) throws ParserConfigurationException {
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		DocumentBuilder db = dbf.newDocumentBuilder();
		DOMImplementation impl = db.getDOMImplementation();
		Document doc = impl.createDocument(null, "flight", null);
		doc.setXmlStandalone(true);
		
		createDifferentStuff(doc);
		createPoints(doc, ds);
		
		return new DOMSource(doc);
	}

	private void createPoints(Document doc, DataSource ds) throws DOMException {
		Element pointsNode = doc.createElementNS(null, "points");
		
		NumberFormat nf = NumberFormat.getNumberInstance(Locale.US);
		nf.setMaximumFractionDigits(12);
		nf.setGroupingUsed(false);
		
		for (int i = 0; i < ds.getSize(); ++i) {
			Element pointNode = doc.createElementNS(null, "point");
			nf.setMinimumFractionDigits(1);
			pointNode.setAttribute("height", nf.format(ds.getValue(ds.getHandle("Altitude"), i)));
			pointNode.setAttribute("id", Integer.toString(i));
			nf.setMinimumFractionDigits(12);
			pointNode.setAttribute("latitude", nf.format(ds.getValue(ds.getHandle("Latitude"), i)));
			pointNode.setAttribute("longitude", nf.format(ds.getValue(ds.getHandle("Longitude"), i)));
			pointsNode.appendChild(pointNode);
		}
		
		doc.getDocumentElement().appendChild(pointsNode);
	}

	private void createDifferentStuff(Document doc) throws DOMException {
		Element baseNode = doc.createElementNS(null, "base");
		Element heightNode = doc.createElementNS(null, "height");
		heightNode.setTextContent("0");
		baseNode.appendChild(heightNode);
		doc.getDocumentElement().appendChild(baseNode);
		
		Element minExcessNode = doc.createElementNS(null, "minExcessNode");
		minExcessNode.setTextContent("0");
		doc.getDocumentElement().appendChild(minExcessNode);
	}
	
}

